package com.iplant.pdm.serviceImpl.dao.fpc;

import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.pdm.service.po.bop.FPCRoutePart;
import com.iplant.pdm.service.po.bop.FPCRouteStation;
import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
@DBBaseDAO(FPCRouteStation.class)
public class FPCRouteStationDAO extends BaseDAO {
	private static FPCRouteStationDAO Instance = null;

	private static Logger logger = LoggerFactory.getLogger(FPCRouteStationDAO.class);

	private FPCRouteStationDAO() {
		super("");
		this.Init();
	}

	public static FPCRouteStationDAO getInstance() {
		if (Instance == null)
			Instance = new FPCRouteStationDAO();
		return Instance;
	}

	public void FPC_UpdateRouteStation(BMSEmployee wLoginUser, FPCRouteStation wRouteStation,
			OutResult<Integer> wErrorCode) {
		wErrorCode.set(0);

		try {
			if (wRouteStation == null || StringUtils.isEmpty(wRouteStation.Name)) {
				wErrorCode.set(ExceptionTypes.Parameter.getValue());
				return;
			}
			FPCRouteStation wRouteStationO = this.FPC_CheckRouteStation(wLoginUser, wRouteStation, wErrorCode);
			if (wRouteStationO.ID > 0) {
				wErrorCode.set(ExceptionTypes.Duplication.getValue());
			}
			if (wErrorCode.Result != 0)
				return;

			Map<String, Object> wParams = new HashMap<String, Object>();

			wParams.put("Version", wRouteStation.Version);
			wParams.put("RoutePartPointID", wRouteStation.RoutePartPointID);

			wParams.put("CERT", wRouteStation.CERT);
			wParams.put("ENVIR", wRouteStation.ENVIR);
			wParams.put("TestMethod", wRouteStation.TestMethod);
			wParams.put("Operation", wRouteStation.Operation);
			wParams.put("StationID", wRouteStation.StationID);

			wParams.put("StationFixturesID", StringUtils.Join(",", wRouteStation.StationFixturesID));

			wParams.put("Description", wRouteStation.Description);

			wParams.put("EditorID", wLoginUser.getID());
			wParams.put("EditTime", Calendar.getInstance());
			wParams.put("Active", wRouteStation.Active);

			if (wRouteStation.ID > 0) {
				wParams.put("ID", wRouteStation.ID);
				super.Update(super.getTableName("fpc_routestation"), "ID", wParams);
			} else {
				wParams.put("CreatorID", wLoginUser.getID());
				wParams.put("CreateTime", Calendar.getInstance());
				wRouteStation.ID = super.Insert(super.getTableName("fpc_routestation"), wParams);
			}

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("FPC_AddRouteStation", ex);
		}
	}

	public void FPC_ActiveRouteStation(BMSEmployee wLoginUser, FPCRouteStation wRouteStation, int wActive,
			OutResult<Integer> wErrorCode) {
		wErrorCode.set(0);
		// 判断客户信息是否存在(中国：统一社会信用代码，国外:提醒是否有重复）

		try {
			if (wRouteStation == null || wRouteStation.ID <= 0) {
				wErrorCode.set(ExceptionTypes.Parameter.getValue());
				return;
			}

			Map<String, Object> wParams = new HashMap<String, Object>();

			wParams.put("ID", wRouteStation.ID);
			wParams.put("EditorID", wLoginUser.getID());
			wParams.put("EditTime", Calendar.getInstance());
			wParams.put("Active", wActive);
			super.Update(super.getTableName("fpc_routestation"), "ID", wParams);

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());

			logger.error("FPC_ActiveRouteStation", ex);
		}
	}

	public List<FPCRouteStation> FPC_QueryRouteStationList(BMSEmployee wLoginUser,int wStationID, String wCode, int wRouteID, int wRoutePartID,
			List<Integer> wRoutePartPointID, String wRoutePartName, String wPartCode, String wRoutePartPointName,
			String wPartPointCode, String wNameLike, int wActive, Pagination wPagination,
			OutResult<Integer> wErrorCode) {
		return this.FPC_QueryRouteStationList(wLoginUser, -1,wStationID, wCode, wRouteID, wRoutePartID, wRoutePartPointID,
				wRoutePartName, wPartCode, wRoutePartPointName, wPartPointCode, wNameLike, wActive, wPagination,
				wErrorCode);
	}

	private List<FPCRouteStation> FPC_QueryRouteStationList(BMSEmployee wLoginUser, int wID,int wStationID, String wCode, int wRouteID,
			int wRoutePartID, List<Integer> wRoutePartPointID, String wRoutePartName, String wPartCode,
			String wRoutePartPointName, String wPartPointCode, String wNameLike, int wActive, Pagination wPagination,
			OutResult<Integer> wErrorCode) {
		List<FPCRouteStation> wRouteStationList = new ArrayList<FPCRouteStation>();
		wErrorCode.set(0);

		try {
			if (wRoutePartPointID == null)
				wRoutePartPointID = new ArrayList<Integer>();

			wRoutePartPointID.removeIf(p -> p <= 0);
			// Step02:查询
			Map<String, Object> wParams = new HashMap<String, Object>();
			String wSQLText = "";
			wSQLText = StringUtils.Format("Select t.*,t0.Name as RoutePartPointName ,t0.Code as RoutePartPointCode,"
					+ " t0.Version as RoutePartPointVersion,t0.RoutePartID, t1.Name as RoutePartName,t1.Code as  RoutePartCode,"
					+ " t1.Version as RoutePartVersion,t1.RouteID,t1.PartCode, t2.Name as RouteName,"
					+ " t2.Code as RouteCode,t2.Version as RouteVersion, t3.Name as CreatorName ,"
					+ " t4.Name as EditorName,t5.Name,t5.Code,group_concat(distinct t6.Name) as StationFixturesName, "
					+ " group_concat(distinct t6.Code) as StationFixturesCode from {0}.fpc_routestation t  "
					+ " left join {0}.fpc_routepartpoint t0 on t.RoutePartPointID=t0.ID "
					+ " left join {0}.fpc_routepart t1 on t0.RoutePartID=t1.ID "
					+ " left join {0}.fpc_route t2 on t1.RouteID=t2.ID "
					+ " left join {0}.mbs_user t3 on t.CreatorID=t3.ID "
					+ " left join {0}.mbs_user t4 on t.EditorID=t4.ID "
					+ " left join {0}.fmc_station t5 on t.StationID=t5.ID "
					+ " left join {0}.fmc_stationfixtrues t6 on find_in_set(t6.ID,t.StationFixturesID) "
					+ " where (:ID<=0 or  t.ID=:ID) and (:Code =''''  or  t5.Code=:Code) AND  (:StationID<=0 or  t.StationID=:StationID) "
					+ " and (:RouteID<=0 or  t1.RouteID=:RouteID) and (:RoutePartID<=0 or  t0.RoutePartID=:RoutePartID) "
					+ " and (:PartCode =''''  or  t1.PartCode=:PartCode) "
					+ " and (:RoutePartName =''''  or  t1.Name=:RoutePartName) "
					+ " and (:PartPointCode =''''  or  t0.PartPointCode=:PartPointCode) "
					+ " and (:RoutePartPointName =''''  or  t0.Name=:RoutePartPointName) "
					+ " AND (:Active<0 or  t.Active=:Active) AND (:NameLike = '''' or  t5.Name like :NameLike)  "
					+ " AND (:RoutePartPointID = '''' or  t.RoutePartPointID IN ({1}) ) group by t.ID ",
					super.getDBName(),
					wRoutePartPointID.size() > 0 ? StringUtils.Join(",", wRoutePartPointID) : "0");

			wSQLText = this.DMLChange(wSQLText);

			wParams.put("ID", wID);
			wParams.put("StationID", wStationID);
			wParams.put("Code", wCode);
			wParams.put("RouteID", wRouteID);
			wParams.put("RoutePartID", wRoutePartID);
			wParams.put("PartCode", wPartCode);
			wParams.put("RoutePartName", wRoutePartName);
			wParams.put("PartPointCode", wPartPointCode);
			wParams.put("RoutePartPointName", wRoutePartPointName);
			wParams.put("RoutePartPointID", StringUtils.Join(",", wRoutePartPointID));
			wParams.put("Active", wActive);
			wParams.put("NameLike", StringUtils.isEmpty(wNameLike) ? "" : ("%" + wNameLike + "%"));

			List<Map<String, Object>> wQueryResultList = super.queryForList(wSQLText, wParams, wPagination);

			FPCRouteStation wRouteStation = null;
			for (Map<String, Object> wSqlDataReader : wQueryResultList) {
				wRouteStation = new FPCRouteStation();
				wRouteStation.ID = StringUtils.parseInt(wSqlDataReader.get("ID"));
				wRouteStation.Code = StringUtils.parseString(wSqlDataReader.get("Code"));
				wRouteStation.Name = StringUtils.parseString(wSqlDataReader.get("Name"));
				wRouteStation.Version = StringUtils.parseString(wSqlDataReader.get("Version"));
				wRouteStation.RoutePartPointID = StringUtils.parseInt(wSqlDataReader.get("RoutePartPointID"));
				wRouteStation.RoutePartPointCode = StringUtils.parseString(wSqlDataReader.get("RoutePartPointCode"));
				wRouteStation.RoutePartPointName = StringUtils.parseString(wSqlDataReader.get("RoutePartPointName"));
				wRouteStation.RoutePartPointVersion = StringUtils
						.parseString(wSqlDataReader.get("RoutePartPointVersion"));
				wRouteStation.RoutePartID = StringUtils.parseInt(wSqlDataReader.get("RoutePartID"));
				wRouteStation.RoutePartCode = StringUtils.parseString(wSqlDataReader.get("RoutePartCode"));
				wRouteStation.RoutePartName = StringUtils.parseString(wSqlDataReader.get("RoutePartName"));
				wRouteStation.RoutePartVersion = StringUtils.parseString(wSqlDataReader.get("RoutePartVersion"));
				wRouteStation.RouteID = StringUtils.parseInt(wSqlDataReader.get("RouteID"));
				wRouteStation.RouteName = StringUtils.parseString(wSqlDataReader.get("RouteName"));
				wRouteStation.RouteCode = StringUtils.parseString(wSqlDataReader.get("RouteCode"));
				wRouteStation.RouteVersion = StringUtils.parseString(wSqlDataReader.get("RouteVersion"));
				wRouteStation.StationID = StringUtils.parseInt(wSqlDataReader.get("StationID"));
				wRouteStation.CERT = StringUtils.parseString(wSqlDataReader.get("CERT"));
				wRouteStation.ENVIR = StringUtils.parseString(wSqlDataReader.get("ENVIR"));
				wRouteStation.TestMethod = StringUtils.parseString(wSqlDataReader.get("TestMethod"));
				wRouteStation.Operation = StringUtils.parseString(wSqlDataReader.get("Operation"));
				wRouteStation.StationFixturesID = StringUtils.parseIntList(wSqlDataReader.get("StationFixturesID"),
						",");
				wRouteStation.StationFixturesName = StringUtils.parseString(wSqlDataReader.get("StationFixturesName"));
				wRouteStation.StationFixturesCode = StringUtils.parseString(wSqlDataReader.get("StationFixturesCode"));

				wRouteStation.Active = StringUtils.parseInt(wSqlDataReader.get("Active"));
				wRouteStation.CreatorID = StringUtils.parseInt(wSqlDataReader.get("CreatorID"));
				wRouteStation.EditorID = StringUtils.parseInt(wSqlDataReader.get("EditorID"));
				wRouteStation.CreatorName = StringUtils.parseString(wSqlDataReader.get("CreatorName"));
				wRouteStation.EditorName = StringUtils.parseString(wSqlDataReader.get("EditorName"));
				wRouteStation.CreateTime = StringUtils.parseCalendar(wSqlDataReader.get("CreateTime"));
				wRouteStation.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));
				wRouteStation.Description = StringUtils.parseString(wSqlDataReader.get("Description"));

				wRouteStationList.add(wRouteStation);
			}

			// Step03:配置系统
		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("FPC_QueryRouteStationList", ex);
		}
		return wRouteStationList;
	}

	public FPCRouteStation FPC_QueryRouteStation(BMSEmployee wLoginUser, int wID, OutResult<Integer> wErrorCode) {
		FPCRouteStation wResult = new FPCRouteStation();
		wErrorCode.set(0);

		try {
			if (wID <= 0)
				return wResult;
			List<FPCRouteStation> wList = this.FPC_QueryRouteStationList(wLoginUser, wID, "", -1, -1, null, "", "", "",
					"", "", -1, Pagination.getDefault(), wErrorCode);

			if (wList != null && wList.size() > 0) {
				wResult = wList.get(0);
			}
		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("FPC_QueryRouteStationByID", ex);
		}
		return wResult;
	}

	public List<FPCRouteStation> FPC_QueryRouteStationList(BMSEmployee wLoginUser, int wRoutePartPointID,
			String wNameLike, OutResult<Integer> wErrorCode) {
		List<FPCRouteStation> wResult = new ArrayList<FPCRouteStation>();
		wErrorCode.set(0);

		try {
			if (wRoutePartPointID <= 0)
				return wResult;

			wResult = this.FPC_QueryRouteStationList(wLoginUser, -1, "", -1, -1,
					StringUtils.parseListArgs(wRoutePartPointID), "", "", "", "", wNameLike, -1,
					Pagination.getDefault(), wErrorCode);

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("FPC_QueryRouteStationList", ex);
		}
		return wResult;
	}

	public List<FPCRouteStation> FPC_QueryRouteStationList(BMSEmployee wLoginUser, int wRoutePartID,
			String wRoutePartPointName, String wPartPointCode, String wNameLike, OutResult<Integer> wErrorCode) {
		List<FPCRouteStation> wResult = new ArrayList<FPCRouteStation>();
		wErrorCode.set(0);

		try {
			if (wRoutePartID <= 0
					|| (StringUtils.isNotEmpty(wRoutePartPointName) && StringUtils.isNotEmpty(wPartPointCode)))
				return wResult;

			wResult = this.FPC_QueryRouteStationList(wLoginUser, -1, "", -1, wRoutePartID, null, "", "",
					wRoutePartPointName, wPartPointCode, "", -1, Pagination.getDefault(), wErrorCode);

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("FPC_QueryRouteStationList", ex);
		}
		return wResult;
	}

	public List<FPCRouteStation> FPC_QueryRouteStationList(BMSEmployee wLoginUser, int wRouteID, String wRoutePartName,
			String wPartCode, String wRoutePartPointName, String wPartPointCode, String wNameLike,
			OutResult<Integer> wErrorCode) {
		List<FPCRouteStation> wResult = new ArrayList<FPCRouteStation>();
		wErrorCode.set(0);

		try {
			if (wRouteID <= 0 || (StringUtils.isNotEmpty(wRoutePartName) && StringUtils.isNotEmpty(wPartCode))
					|| (StringUtils.isNotEmpty(wRoutePartPointName) && StringUtils.isNotEmpty(wPartPointCode)))
				return wResult;

			wResult = this.FPC_QueryRouteStationList(wLoginUser, -1, "", wRouteID, -1, null, wRoutePartName, wPartCode,
					wRoutePartPointName, wPartPointCode, wNameLike, -1, Pagination.getDefault(), wErrorCode);

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("FPC_QueryRouteStationList", ex);
		}
		return wResult;
	}

	public FPCRouteStation FPC_CheckRouteStation(BMSEmployee wLoginUser, FPCRouteStation wFPCRouteStation,
			OutResult<Integer> wErrorCode) {
		FPCRouteStation wRouteStation = new FPCRouteStation();
		wErrorCode.set(0);

		try {

			// Step0:查询
			Map<String, Object> wParams = new HashMap<String, Object>();
			String wSQLText = StringUtils.Format(
					"Select * from {0}.fpc_routestation where ID!=:ID "
							+ " and  (RoutePartPointID=:RoutePartPointID and StationID =:StationID )  ",
					super.getDBName());
			wParams.clear();
			wParams.put("ID", wFPCRouteStation.ID);
			wParams.put("StationID", wFPCRouteStation.StationID);
			wParams.put("RoutePartPointID", wFPCRouteStation.RoutePartPointID);

			wSQLText = this.DMLChange(wSQLText);
			List<Map<String, Object>> wQueryResultList = super.queryForList(wSQLText, wParams);
			for (Map<String, Object> wSqlDataReader : wQueryResultList) {
				wRouteStation.ID = StringUtils.parseInt(wSqlDataReader.get("ID"));
				wRouteStation.Name = StringUtils.parseString(wSqlDataReader.get("Name"));
				wRouteStation.Code = StringUtils.parseString(wSqlDataReader.get("Code"));
				wRouteStation.StationID = StringUtils.parseInt(wSqlDataReader.get("StationID"));

				wRouteStation.CERT = StringUtils.parseString(wSqlDataReader.get("CERT"));
				wRouteStation.ENVIR = StringUtils.parseString(wSqlDataReader.get("ENVIR"));
				wRouteStation.TestMethod = StringUtils.parseString(wSqlDataReader.get("TestMethod"));
				wRouteStation.Operation = StringUtils.parseString(wSqlDataReader.get("Operation"));
				wRouteStation.CreatorID = StringUtils.parseInt(wSqlDataReader.get("CreatorID"));
				wRouteStation.CreateTime = StringUtils.parseCalendar(wSqlDataReader.get("CreateTime"));
				wRouteStation.EditorID = StringUtils.parseInt(wSqlDataReader.get("EditorID"));
				wRouteStation.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));
				wRouteStation.Active = StringUtils.parseInt(wSqlDataReader.get("Active"));

			}

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("FPC_CheckRouteStation", ex);
		}
		return wRouteStation;
	}

}
